home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / epsilon / react.py < prev    next >
Text File  |  2009-03-13  |  1KB  |  36 lines

  1. # -*- test-case-name: epsilon.test.test_react -*-
  2. # Copyright (c) 2008 Divmod.  See LICENSE for details.
  3.  
  4. """
  5. Utilities for running the reactor for a while.
  6. """
  7.  
  8. from twisted.python.log import err
  9.  
  10.  
  11. def react(reactor, main, argv):
  12.     """
  13.     Call C{main} and run the reactor until the L{Deferred} it returns fires.
  14.  
  15.     @param reactor: An unstarted L{IReactorCore} provider which will be run and
  16.         later stopped.
  17.  
  18.     @param main: A callable which returns a L{Deferred}.  It should take as
  19.         many arguments as there are elements in the list C{argv}.
  20.  
  21.     @param argv: A list of arguments to pass to C{main}.
  22.  
  23.     @return: C{None}
  24.     """
  25.     stopping = []
  26.     reactor.addSystemEventTrigger('before', 'shutdown', stopping.append, True)
  27.     finished = main(reactor, *argv)
  28.     finished.addErrback(err, "main function encountered error")
  29.     def cbFinish(ignored):
  30.         if not stopping:
  31.             reactor.callWhenRunning(reactor.stop)
  32.     finished.addCallback(cbFinish)
  33.     reactor.run()
  34.  
  35.  
  36.